package com.ruoyi.web.controller.system;

import java.io.*;
import java.util.*;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.service.ITmcUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ITmcHouseInfoService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 房屋产权信息
 * Controller
 *
 * @author ruoyi
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/system/info")
public class TmcHouseInfoController extends BaseController {
    @Autowired
    private ITmcHouseInfoService tmcHouseInfoService;
    @Autowired
    private ITmcUserService tmcUserService;

    /**
     * 查询房屋产权信息
     * 列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TmcUserHouse tmcUserHouse) {
        startPage();
        List<TmcUserHouse> list = tmcHouseInfoService.selectTmcHouseInfoList(tmcUserHouse);
        return getDataTable(list);
    }

    /**
     * 查询房屋产权信息
     * 列表
     */
    @GetMapping("/dept/list")
    public TableDataInfo Deptlist() {
        startPage();
        List<Dept> list = tmcHouseInfoService.queryDept();
        return getDataTable(list);
    }


    /**
     * 获取房屋产权信息
     * 详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tmcHouseInfoService.selectTmcHouseInfoById(id));
    }

    /**
     * 新增房屋产权信息
     */
    @Log(title = "房屋产权信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmcUserHouse tmcUserHouse) {
        return toAjax(tmcHouseInfoService.insertTmcHouseInfo(tmcUserHouse));
    }

    /**
     * 修改房屋产权信息
     */
    @Log(title = "房屋产权信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmcUserHouse tmcUserHouse) {
        return toAjax(tmcHouseInfoService.updateTmcHouseInfo(tmcUserHouse));
    }

    /**
     * 删除房屋产权信息
     */
    @Log(title = "房屋产权信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tmcHouseInfoService.deleteTmcHouseInfoByIds(ids));
    }

    @ApiOperation(value = "项目导入")
    @Log(title = "项目导入", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public AjaxResult Upload(MultipartFile file) {
        if (tmcHouseInfoService.importExcel(file) > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @Log(title = "【費用管理】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        OutputStream os = null;
        InputStream is = null;
        OutputStream out = null;
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            // 取得输出流
            os = response.getOutputStream();
            // 清空输出流
            response.reset();
            response.setContentType("application/x-download;charset=GBK");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("项目明细模板".getBytes("utf-8"), "iso-8859-1"));
            //读取流
            File f = new File(RuoYiConfig.getProfile()+"//excel//房屋导入模板.xlsx");
            byte[] byte1 = File2byte(f);
            out = response.getOutputStream();
            bos = new BufferedOutputStream(out);
            bos.write(byte1);
            bos.flush();
        } catch (IOException e) {
            AjaxResult.error("下载附件失败,error:" + e.getMessage());
        }
        //文件的关闭放在finally中
        finally {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {

            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {

            }
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {

            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {

            }
            try {
                if (os != null) {
                    os.close();
                }

            } catch (IOException e) {

            }
        }
        AjaxResult.success();
    }

    //    查询用户的房屋账单
    @PostMapping("/QueryUserHouseBillList")
    public TableDataInfo QueryUserHouseBillList(@RequestBody Map map) {
        List<TmcHouseBill> list = tmcHouseInfoService.selectTmcHouseBillList(Long.valueOf(map.get("userID") + ""), Long.valueOf(map.get("houseID") + ""));
        return getDataTable(list);
    }

    //    查询用户最新的房屋账单
    @PostMapping("/QueryUserHouseLastestBillInfo")
    public AjaxResult QueryUserHouseLastestBillInfo(@RequestBody Map map) {
        TmcHouseBill tmcHouseBill = tmcHouseInfoService.selectTmcHouseBill(Long.valueOf(map.get("userID") + ""), Long.valueOf(map.get("houseID") + ""));
        if (null != tmcHouseBill) {
            return AjaxResult.success(tmcHouseBill);
        } else {
            return AjaxResult.success("[]");
        }
    }

    //    查询单个最新的房屋账单
    @GetMapping("/QueryUserHouseBillInfo")
    public AjaxResult QueryUserHouseBillInfo(Long recordID) {
        return AjaxResult.success(tmcHouseInfoService.selectTmcHouseBillById(recordID));
    }

    //   立即缴费
    @PostMapping("/updateUserHouseBillInfoByid")
    public AjaxResult updateUserHouseBillInfoByid(Long recordID) {
        if (tmcHouseInfoService.updateUserHouseBillInfoByid(recordID) == 1) {
            return AjaxResult.success("缴费成功");
        } else {
            return AjaxResult.error("缴费失败");
        }

    }

    @GetMapping("/queryTmcHouseBillList")
    public TableDataInfo queryTmcHouseBillList(TmcHouseBill tmcHouseBill) {
        startPage();
        List<TmcHouseBill> list = tmcHouseInfoService.queryTmcHouseBillList(tmcHouseBill);
        return getDataTable(list);
    }
    @GetMapping("/queryBill")
    public List queryBill(Long companyId,String year){
        startPage();
        List map = tmcHouseInfoService.queryBill(companyId,year);
        return map;
    }
    //文件转数组
    public static byte[] File2byte(File tradeFile) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
